/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vadung.contragame2d;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author dung.voanh
 */
public class MyPlayer extends MyActor {

    private int m_iJumpHeight;
    private boolean m_isJump;
    private boolean m_isFall;
    private boolean m_isGoback;
    private int m_iYJumpMax;
    private int m_iDetalX;
    private int m_iDetalY;
    private int m_iMaxBullet; //Max bullet shot once time

    public MyPlayer(int[] aiSpriteID) {
        super(aiSpriteID);
        Init();
    }

    public MyPlayer(String[] astrSpriteName) {
        super(astrSpriteName);
        Init();
    }

    private void Init() {
        m_isJump = false;
        m_isFall = true;
        m_isGoback = false;
        m_iJumpHeight = 60;
        m_iYJumpMax = m_iY - m_iJumpHeight;
        m_iDetalX = 4;
        m_iDetalY = 6;
        m_isDie = false;
        m_iMaxBullet = 4;
    }

    public void Render(Graphics g) {
        super.Render(g); //To change body of generated methods, choose Tools | Templates.
        if (m_isDie) {
            int width = Midlet.s_canGame.getWidth();
            g.setColor(0, 0, 0);
            g.fillRect(0, 80, width, 60);
            g.setColor(255, 0, 0);
            g.drawString("Hero is died!", width / 2, 100, Graphics.HCENTER | Graphics.TOP);
            //System.out.println("Draw string: Hero is died.");
        }
    }

    public void Update() {
        if (m_isDie) {
            return;
        }

        MySprite m_sprPlayer = this.GetCurrentSpriteAction();
        GameLayerManager layerManager = GameLayerManager.GetInstance();
        int key = Midlet.s_canGame.GetKeyPressAction();
        //System.out.println("Key action: " + key);
        // Player fall endless...
        if (layerManager.IsOutMap(m_iXMap, m_iYMap + m_iHeight)) {
            System.out.println("Player is died.");
            m_isDie = true;
        }

        if (m_isJump) {
            this.MoveOnMap(0, -m_iDetalY);
            if (m_iY <= m_iYJumpMax) {
                m_isJump = false;
                m_isFall = true;
            }
        }
        if (m_isFall) {
            this.MoveOnMap(0, m_iDetalY);
            // When player collision obstacle, stop fall
            boolean isCollision = layerManager.CheckCollision(1, m_iXMap + 5, m_iYMap, m_iWidth - 10, m_iHeight);
            if (isCollision) {
                m_isFall = false;
                boolean isCollisionAtop = layerManager.CheckCollision(1, m_iXMap + m_iWidth / 2, m_iYMap + layerManager.GetTiledHeight());
                if (isCollisionAtop) {
                    m_isFall = true;
                }
            }
        }
        m_state = DEFINE.ACTOR_STATE_NORMAL;

        int keyTouch = GetTouchKey();

        if (key == 0 && keyTouch == 0) {
            if (m_isGoback) {
                m_sprPlayer.SetCurrentFrame(5);
            } else {
                m_sprPlayer.SetCurrentFrame(4);
            }
        } else {
            if (keyTouch != 0) {
                HandelLeft(keyTouch);

                HandelRight(keyTouch);

                HandelUp(keyTouch);

                HandelDown(keyTouch);

                HandelFire(keyTouch);

            }

            if (key != 0) {
                HandelLeft(key);

                HandelRight(key);

                HandelUp(key);

                HandelDown(key);

                HandelFire(key);

            }
        }
    }

    private void HandelLeft(int key) {
        MySprite m_sprPlayer = this.GetCurrentSpriteAction();
        GameLayerManager layerManager = GameLayerManager.GetInstance();
        if ((key & DEFINE.KEY_LEFT) == DEFINE.KEY_LEFT) {
            m_state = DEFINE.ACTOR_STATE_MOVE_LEFT;
            m_isGoback = true;
            if (m_sprPlayer.GetCurrentFrame() <= 5) {
                m_sprPlayer.SetCurrentFrame(9);
            } else {
                m_sprPlayer.PreviousFrame();
            }

            if (GetX() <= 20) {
                return;
            }
            if (layerManager.CheckCollision(1, m_iXMap, m_iYMap, 0, m_iHeight - layerManager.GetTiledHeight())) {
            } else {
                this.MoveOnMap(-m_iDetalX, 0);
                if (GetX() <= Midlet.s_canGame.getWidth() / 2) {
                    layerManager.MoveViewWindows(-m_iDetalX, 0);
                }
            }
            //Fall when local bottom is empty
            if (m_isFall == false && m_isJump == false) {
                m_isFall = !layerManager.CheckCollision(1, m_iXMap, m_iYMap, m_iWidth - 10, m_iHeight);
            }
        }
    }

    private void HandelRight(int key) {
        MySprite m_sprPlayer = this.GetCurrentSpriteAction();
        GameLayerManager layerManager = GameLayerManager.GetInstance();

        if ((key & DEFINE.KEY_RIGHT) == DEFINE.KEY_RIGHT) {
            m_state = DEFINE.ACTOR_STATE_MOVE_RIGHT;
            m_isGoback = false;
            if (layerManager.CheckCollision(1, m_iXMap + m_iDetalX, m_iYMap, m_iWidth, m_iHeight - layerManager.GetTiledHeight())) {
            } else {
                this.MoveOnMap(m_iDetalX, 0);
                if (GetX() >= Midlet.s_canGame.getWidth() / 2) {
                    layerManager.MoveViewWindows(m_iDetalX, 0);
                }
            }
            if (m_sprPlayer.GetCurrentFrame() >= 4) {
                m_sprPlayer.SetCurrentFrame(0);
            } else {
                m_sprPlayer.NextFrame();
            }

            //Fall when local bottom is empty
            if (m_isFall == false && m_isJump == false) {
                m_isFall = !layerManager.CheckCollision(1, m_iXMap + 5, m_iYMap, m_iWidth - 12, m_iHeight);
            }
        }
    }

    private void HandelUp(int key) {
        MySprite m_sprPlayer = this.GetCurrentSpriteAction();
        GameLayerManager layerManager = GameLayerManager.GetInstance();

        if ((key & DEFINE.KEY_UP) == DEFINE.KEY_UP) {
            m_state = DEFINE.ACTOR_STATE_UP;
            if (m_isFall == false && m_isJump == false) {
                m_iYJumpMax = m_iY - m_iJumpHeight;
                m_isJump = true;
                Midlet.s_canGame.RemoveKeyAction(DEFINE.KEY_UP);
            }
        }
    }

    private void HandelDown(int key) {
        MySprite m_sprPlayer = this.GetCurrentSpriteAction();
        GameLayerManager layerManager = GameLayerManager.GetInstance();

        if ((key & DEFINE.KEY_DOWN) == DEFINE.KEY_DOWN) {
            m_state = DEFINE.ACTOR_STATE_DOWN;
            if (m_isFall == false && m_isJump == false) {
                boolean isCollision = layerManager.CheckCollision(1, m_iXMap, m_iYMap + layerManager.GetTiledHeight(), m_iWidth, m_iHeight);
                if (isCollision == false) {
                    this.MoveOnMap(0, layerManager.GetTiledHeight());
                    m_isJump = false;
                    m_isFall = true;
                }
                Midlet.s_canGame.RemoveKeyAction(DEFINE.KEY_DOWN);
            }
        }
    }

    private void HandelFire(int key) {
        MySprite m_sprPlayer = this.GetCurrentSpriteAction();
        GameLayerManager layerManager = GameLayerManager.GetInstance();

        if ((key & DEFINE.KEY_FIRE) == DEFINE.KEY_FIRE) {
            if (m_iMaxBullet > MyObject.GetInstance().GetSumBullet(this)) {
                int aiSpriteID[] = new int[]{1};
                MyBullet bullet = new MyBullet(this, aiSpriteID);
                bullet.SetPositionOnMap(m_iXMap + m_iWidth / 2, m_iYMap + m_iHeight / 4);
                //Set direction fly for bullet
                if (m_isGoback) {
                    bullet.m_state = DEFINE.ACTOR_STATE_MOVE_LEFT;
                }

                MyObject.GetInstance().Add(bullet);
                Midlet.s_canGame.RemoveKeyAction(DEFINE.KEY_FIRE);

            }
        }
    }

    //Tuoch...
    private int GetTouchKey() {
        if (GLOBAL.IS_TOUCH == false) {
            return 0;
        }

        int key = 0;
        MySprite m_sprPlayer = this.GetCurrentSpriteAction();
        GameLayerManager layerManager = GameLayerManager.GetInstance();
        int m_iDesX = GLOBAL.TOUCH_X + layerManager.GetPositionX();
        int m_iDesY = GLOBAL.TOUCH_Y + layerManager.GetPositionY();

        if (m_iDesX < m_iXMap) {
            if (m_iDesY > m_iYMap - 20 && m_iDesY < m_iYMap + m_iHeight + 20) {
                key |= DEFINE.KEY_LEFT;

            } else if (m_iDesY < m_iYMap && m_iDesX > m_iXMap - 20 && m_iDesX < m_iXMap + m_iWidth + 20) {
                key |= DEFINE.KEY_UP;

            } else if (m_iDesY < m_iYMap) {
                key |= DEFINE.KEY_LEFT;
                key |= DEFINE.KEY_UP;

            } else {
                key |= DEFINE.KEY_LEFT;
                key |= DEFINE.KEY_DOWN;
            }
        } else {
            if (m_iDesY > m_iYMap - 20 && m_iDesY < m_iYMap + m_iHeight + 20) {
                key |= DEFINE.KEY_RIGHT;

            } else if (m_iDesY > m_iYMap && m_iDesX > m_iXMap - 20 && m_iDesX < m_iXMap + m_iWidth + 20) {
                key |= DEFINE.KEY_DOWN;

            } else if (m_iDesY < m_iYMap) {
                key |= DEFINE.KEY_RIGHT;
                key |= DEFINE.KEY_UP;
            } else {
                key |= DEFINE.KEY_RIGHT;
                key |= DEFINE.KEY_DOWN;
            }
        }
        return key;
    }
}
